﻿using NUnit.Framework;
using Selenium;
using SeleniumToolkit.Config;

namespace SeleniumToolkit.NUnit.Tests
{
    public class WebTestCaseSpecs : ContextSpecification
    {
        protected ISelenium browser;
        protected Browser browserInfo;

        protected string defaultUrl;
        protected string defaultBrowser;
        protected int defaultTimeout;
        protected bool fixtureOwnsBrowserProcess;

        protected override void Context_BeforeAllSpecs()
        {
            // read the config out into variables so tests can access them easier
            /* When running suite programmatically, config is unavailable.
            var config = SeleniumConfiguration.GetConfig();
            */
            var config = new SeleniumConfiguration();
            config.AddinSettings.RecylePerFixture = false;

            defaultUrl = config.BrowserUrl;
            defaultBrowser = config.DefaultBrowser;
            defaultTimeout = config.Timeout;
            fixtureOwnsBrowserProcess = config.AddinSettings.RecylePerFixture;
        }

        public class when_testcase_owns_process : WebTestCaseSpecs
        {
            protected override void Context_BeforeAllSpecs()
            {
                base.Context_BeforeAllSpecs();

                Browser.Current.ShouldBeNull();
            }

            protected override void Context()
            {
                Browser.Current.ShouldNotBeNull();
                browser = Browser.Current;
                browserInfo = Browser.BrowserInfo;
                base.Context();
            }
        }


        [WebFixture]
        public class when_settings_are_defined_on_the_testcase : when_testcase_owns_process
        {
            // #1
            [WebTest("*firefox")]
            public void browserprofile_should_be_applied()
            {
                browserInfo.BrowserString.ShouldEqual("*firefox");
            }

            // #2
            [WebTest(BaseUrl = "http://www.amazon.com")]
            public void base_url_should_be_applied()
            {
                browser.Open("/");
                browserInfo.BaseUrl.ShouldEqual("http://www.amazon.com");
                browser.GetLocation().ShouldContain("amazon.com");
            }
        }

        [WebFixture]
        public class when_testcase_has_no_settings : when_testcase_owns_process
        {
            // #3
            [WebTest]
            public void configuration_setttings_should_be_used()
            {
                browserInfo.BaseUrl.ShouldEqual(defaultUrl);
                browserInfo.BrowserString.ShouldEqual(defaultBrowser);
                browserInfo.Timeout.ShouldEqual(defaultTimeout.ToString());
            }
        }
    }
}
